package com.example.acer.im.helper;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.SparseArray;
import android.view.MenuItem;


/**
 * Created by HongQian.Wang on 2017/9/17.
 * 解决对fragment的调度问题
 */

public class NacHelper<T> {
    private final SparseArray<Tab<T>> tabs = new SparseArray();
    private final int containerId;
    private final Context context;
    private final FragmentManager manager;
    private OnTabChangedListener<T> listener;
    private Tab<T> currentTab;



    public NacHelper(Context context, int containerId, FragmentManager manager, OnTabChangedListener<T> listener) {
        this.containerId = containerId;
        this.context = context;
        this.manager = manager;
        this.listener = listener;
    }

    public NacHelper<T> add(int menuId ,Tab<T> tab){
            tabs.put(menuId,tab);

            return this;
    }

    public Tab<T> getCurrentTab(){
        return  currentTab;
    }


    public boolean performClickMenu(MenuItem item) {

        Tab<T> tab = tabs.get(item.getItemId());
         if(tab!=null) {
            doSelect(tab);
             return  true;
         }

        return false;
    }

    /**
     * 进行真实的Tab选择操作
     * @param tab
     */
    private void doSelect(Tab<T> tab) {
               Tab<T>  oldTab=null;
               if(currentTab !=null) {
                    oldTab=currentTab;
                    if(oldTab==tab) {
                        notifyReselect(tab);
                        return;
                    }
               }
               currentTab=tab;
               doTabChanged(currentTab,oldTab);
    }

    private void doTabChanged(Tab<T> newTab, Tab<T> oldTab) {
        FragmentTransaction ft = manager.beginTransaction();
              if(oldTab!=null) {
                       if(oldTab.fragment!=null) {
                                 ft.detach(oldTab.fragment);  //将fragment从界面中移除 但还在FragmentManager的缓存空间中
                           
                       }
              }
              if(newTab!=null) {
                       if(newTab.fragment==null) {
                           //创建Fragment
                           Fragment fragment = Fragment.instantiate(context, newTab.clx.getName());
                           newTab.fragment=fragment;
                           ft.add(containerId,fragment,newTab.clx.getName());
                       }else {
                                 ft.attach(newTab.fragment);
                       }
              }

        ft.commit();
        notifySelect(newTab,oldTab);
    }

    /**
     * 二次点击
     * @param tab
     */
    private void notifyReselect(Tab<T> tab) {

    }
    /**
     * 一次点击
     * @param oldTab
     */
    private void notifySelect(Tab<T> newTab, Tab<T> oldTab) {
        if(listener!=null) {
            listener.onTabChanged(newTab,oldTab);
        }

    }
    public static class Tab<T> {
        public Class<?> clx;
        //用户自己设定需要使用 ，额外字段
        public T extra;

        //内部缓存的对应的Fragment
        Fragment fragment;

        public Tab(Class<?> clx, T extra) {
            this.clx = clx;
            this.extra = extra;
        }
    }

    //定义事件处理完成后的回调接口
    public interface OnTabChangedListener<T> {
        void onTabChanged(Tab<T> newTab, Tab<T> oldTab);
    }

}
